package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import controller.ConnectionTester;
import accounts.Authorization;

public abstract class MainPanel extends JPanel {
	protected JPanel attributesPanel;
	protected Toolbar toolbar;
	protected JMenuBar menuBar;
	protected JMenu menuFile, menuEdit, menuMode;
	protected JMenuItem menuItem_LogOut, menuItem_Exit;
	protected JLabel toolbarLabel, attributesLabel;
	protected CanvasPanel canvas;
	protected Authorization authorization;
	
	public MainPanel(){
		this.setLayout(new BorderLayout(10, 10));
		//this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
		this.authorization = ConnectionTester.getAuthorization();
		
		//menu Stuff:
		menuBar = new JMenuBar();
		menuFile = new JMenu("File");
		menuFile.setMnemonic(KeyEvent.VK_F);
		menuFile.getAccessibleContext().setAccessibleDescription("File");
		
		if(ConnectionTester.getMode() != Mode.SECURITY){
			menuEdit = new JMenu("Edit");
			menuEdit.setMnemonic(KeyEvent.VK_E);
			menuEdit.getAccessibleContext().setAccessibleDescription("Edit");
		}
		
		if(authorization == Authorization.ADMINISTRATOR){
			menuMode = new JMenu("Mode");
			menuMode.setMnemonic(KeyEvent.VK_M);
			menuMode.getAccessibleContext().setAccessibleDescription("Change app mode to create or simulation");
		}
		
		toolbar = new Toolbar();
		setupMenuBar();
		menuItem_LogOut = new JMenuItem("Log Out");
		menuItem_LogOut.addActionListener(new LogOutActionListener());
		menuFile.add(menuItem_LogOut);
		menuItem_Exit = new JMenuItem("Exit");
		menuItem_Exit.addActionListener(new ExitActionListener());
		menuFile.add(menuItem_Exit);
		
		menuBar.add(menuFile);
		if(ConnectionTester.getMode() != Mode.SECURITY){
			menuBar.add(menuEdit);
		}
		if(authorization == Authorization.ADMINISTRATOR){
			menuBar.add(menuMode);
		}
		
		//toobarPanel Stuff:
		setupToolbar();
		this.add(toolbar, BorderLayout.LINE_START);
		
		//canvasPanel Stuff:
		canvas = new CanvasPanel();
		setupCanvasPanel();
		this.add(canvas, BorderLayout.CENTER);
		
		//attributesPanel Stuff:
		attributesPanel = new AttributesPanel();
		setupAttributesPanel();
		this.add(attributesPanel, BorderLayout.LINE_END);
		
		
	}
	
	public abstract void setupMenuBar();
	public abstract void setupToolbar();
	public abstract void setupCanvasPanel();
	public abstract void setupAttributesPanel();
	
	private class ExitActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			System.out.print("Exiting");
			MainFrame.exit();
		}
	}
	
	private class LogOutActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			System.out.print("Logging Out");
			MainFrame.LaunchLogin();
		}
	}
	
	public JMenuBar getMenuBar(){
		return menuBar;
	}
	
	public void refreshAttributesPanel(){
		((AttributesPanel)attributesPanel).setupAttributesPanel();
		MainFrame.refresh();
	}
}
